/**
 * @desc
 *  编译webpack对组件条进行打包处理，然后按指定的目录上传到阿里云上去．
 *
 *  步骤1:webpack打包
 *  步骤2:对打包后的内容上传到阿里云上去
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/4/6
 **/
'use strict';

let path  =require('path');
let fse  =require('fs-extra');
var Immutable  =require('immutable');
let widetUtil =require('@qianmi/x-site-common/util/widget-util');
let fileUtil =require('@qianmi/x-site-common/util/file-util');
let _util = require('./_util');

let desiginerN = _util.getWorbenchName();
let compMap;
if(compMap){
    module.exports=compMap;
}

compMap={};
// gulp widgetSync-prod    生产环境
// gulp widgetSync-prod-gray    灰度环境
// npm run buildBar && gulp widgetSync-test     测试环境

let ignoreDirs = [
   path.join(__dirname,'../node_modules'),
   path.join(__dirname,'../settings'),
   path.join(__dirname,'../statics'),
   path.join(__dirname,'../dist'),
];

fileUtil.traceDirTree(path.join(__dirname,'..'),(filePath)=>{
   
    for(let i =0,ilen=ignoreDirs.length ; i<ilen; i++){
        if(filePath.startsWith(ignoreDirs[i])){
            return ;
        }
    }
    
    if (filePath.endsWith('-p.js')) { //是组件条的情况
        var compFilePath = filePath.replace('-p.js', '').replace('.js', '')
           .replace(new RegExp('.*'+desiginerN), desiginerN);
    
        // widgetNameSpace
        // var widgetNameSpace = filePath.replace('-p.js', '').replace('.js', '')
        //    .replace(new RegExp('.*'+__base), desiginerN);
        var widgetName = widetUtil.extraWidgetNameFromPath(compFilePath);
        //
        if(compMap[compFilePath]){
            console.log(compFilePath,compMap[compFilePath]);
            throw new Error('组件名重复,请修正!!'+compFilePath);
        } else {
            //其所引用的组件
           let dependenciesBar = searchNameSpaceFromFile({widgetNameSpace:compFilePath}).toJS();
            compMap[compFilePath] = {
                widgetName: widgetName,
                widgetPath: compFilePath,
                widgetNameSpace:compFilePath,
                dependenciesBar
            };
            console.log(compMap[compFilePath]);
        }
    }
});
console.dir(compMap);

module.exports=compMap;
 

/**
 * 根据指定组件 找到其直接或间接依赖的子组件
 * @param widgetNameSpace 组件命名空间
 * @param topDir 顶部目录
 **/
function searchNameSpaceFromFile({widgetNameSpace}){
    //console.log('widgetNameSpace::',widgetNameSpace);
    let fileContent  = fse.readFileSync(path.join(__dirname,'../../',widgetNameSpace+"-p.js"));
    //找到default中对子组件的引用
    var matchResult = fileContent.toString().match(/widgetNameSpace[^,\n]*/g);
    
    if(matchResult && matchResult.length > 0){
        let result = Immutable.Set();
        matchResult.forEach(item=>{
            let value  = item.trim().replace(" ","","g").replace("widgetNameSpace","").replace(":",'').replace(/['|"]/g,"").trim();
            result =result.add(value);
        });
        
        if(result.count() > 0 ){
            var result2  =Immutable.Set();
            result.map(subItem=>{
                result2=result2.union(searchNameSpaceFromFile({widgetNameSpace:subItem}));
            });
            return result.union(result2);
        }else{
            return result;
        }
    }else{
        return Immutable.Set();
    }
}
